<?php
protected $_requestUrl = null;
protected $_requestUrlData = array();
protected $_menu = array();

protected $_menuData = array();

public $position = 'top';
 
public function init(){
	$this->activeCssClass = 'select';
	$this->_requestUrl = '/'.Yii::app()->request->getPathInfo();
	$this->_requestUrlData = $this->requestUrlData($this->_requestUrl);
	$this->_menu = include Yii::getPathOfAlias('application.config.menu').'.php';
	parent::init();
}

protected function requestUrlData($url){
	$temp = explode('/',$url);
	return array(
			'controller' => $temp[1],
			'action'     => $temp[2],
	);
}

public function buildMenu(){
	$menuData = array();
	$menu = $this->_menu;
	if ($this->position == 'left'){
		$menu = $menu[$this->_requestUrlData['controller']]['items'];
		 
	}
	$menuData = $this->buildMenuItem($menu);

	return $menuData;
}

public function buildMenuItem($menu){
	$menuData = array();
	foreach ($menu as $menuKey => $menu){
		$menuData[$menuKey] = $menu;

		if (array_key_exists('url',$menu)){
			$tempUrl = $this->requestUrlData($menu['url']);
			$menuData[$menuKey]['active'] = $tempUrl['action'] == $this->_requestUrlData['action'];
			$menuData[$menuKey]['url'] = Yii::app()->request->baseUrl.$menu['url'];
		}else{
			$menuData[$menuKey]['active'] = false;
		}



		$menuData[$menuKey]['itemOptions'] = array('class' => 'dir');
		if (array_key_exists('items',$menu)){
			$menuData[$menuKey]['items'] = $this->buildMenuItem($menu['items']);
		}
	}
	return $menuData;
}

public function run()
{
	//$this->items
	$this->items = $this->buildMenu();

	parent::run();
}

protected function renderMenuItem($item)
{
	if(isset($item['url']))
	{
		$label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);

		$itemTag = null;
		if(!empty($item['icon'])){
			$itemTag .= CHtml::tag('span',array('class'=>'icon'),CHtml::tag('img',array('width' => 22,'height' => 23,'alt'=>$label,'src' => Yii::app()->baseUrl.$item['icon'],'style'=>'margin-top:0px;')));
		}
		 
		$itemTag .= CHtml::tag('span',array('title' => $label),$label);
		 
		return CHtml::link($itemTag,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
	}
	else
		return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}